const isFunction = fn => typeof fn === "function";
const PENDING = "PENDING";
const FULFILLED = "FULFILLED";
const REJECTED = "REJECTED";
class MyPromise {
    constructor(func) {
        if (!isFunction) {
            throw new Error("MyPromise must accept an function");
        }
        // 添加状态和值
        this._status = PENDING;
        this._value = undefined;
        // 添加成功回调函数队列 
        // (同一个promise可以多次使用then函数,来注册成功时的回调函数,当状态改变为FULFILLED的时候,会依次执行这里面的函数)
        this._fulfilledQueues = []
        // 添加失败回调函数队列
        this._rejectedQueues = []
        // 创建Promise的时候就执行func, 该函数的参数是两个函数,
        // 一个执行的时候能够将状态改变为FULFILLED
        // 一个执行的时候能够将状态改变为REJECTED
        func((val) => this._resolve(val), (val) => this._rejecct(val))
    }
    _resolve(val) {
        if (this._status === PENDING) {
            return;
        }
        this._status = FULFILLED;
        this._value = val;
    }
    _rejecct(val) {
        if (this._status === PENDING) {
            return;
        }
        this._status = REJECTED;
        this._value = val;
    }
    then(onFulfilled, onRejected) {
        if (this._status === PENDING) {
            if (isFunction(res))
                this._fulfilledQueues.push(res);
            if (isFunction(rej))
                this._rejectedQueues.push(rej);
        }

    }
}